package com.ccjq.shosestore.product.service.impl;

import com.ccjq.shosestore.core.domain.PageInfo;
import com.ccjq.shosestore.product.dao.ProductDao;
import com.ccjq.shosestore.product.dao.impl.ProductDaoImpl;
import com.ccjq.shosestore.product.model.BrandModel;
import com.ccjq.shosestore.product.model.ProductModel;
import com.ccjq.shosestore.product.service.ProductService;
import com.ccjq.shosestore.product.util.PageBean;
import com.ccjq.shosestore.product.util.PageHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


public class ProductServiceImpl implements ProductService {

	public PageInfo queryProducts(String categoryId, int pageSize, int pageNo) {
		ProductDao productDao = new ProductDaoImpl();
		List<ProductModel> products = null;
		int startRow = 1;
		int total = 0;
		if (pageNo - 1 > 0) {
			startRow = (pageNo - 1) * pageSize + 1;
		}
		if (StringUtils.isNotBlank(categoryId)) {
			products = productDao.queryAll(categoryId, pageSize, startRow);
			total = productDao.getBrandRow(categoryId);
		} else {
			products = productDao.queryAll(pageSize, startRow);
			total = productDao.getProductListRow();
		}

		int pages = 1;
		if (total % pageSize == 0) {
			pages = total / pageSize;
		} else {
			pages = total / pageSize + 1;
		}
		PageInfo page = new PageInfo();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setPages(pages);
		page.setTotal(total);
		page.setObjs(products);
		return page;
	}


	public List<ProductModel> getScroll() {
		ProductDao p = new ProductDaoImpl();
		List<ProductModel> products = p.getScrollList();
		return products;
	}


	public List<ProductModel> getProductList(String pageMethod, String currentPage) {
		ProductDao p = new ProductDaoImpl();
		int totalRows = p.getProductListRow();
		System.out.println("总行数：" + totalRows);
		// 用户要执行的动作 pageMethod
		// 当前页 currentPage
		PageBean pageBean = PageHelper.getPager(currentPage, pageMethod, totalRows);
		List<ProductModel> list = p.getProductList(pageBean.getStartRow(), pageBean.getPageSize());
		return list;
	}


	public List<ProductModel> getBrand(String Category_id, String pageMethod, String currentPage) {
		ProductDao p = new ProductDaoImpl();
		int totalRows = p.getBrandRow(Category_id);
		System.out.println("总行数：" + totalRows);
		// 用户要执行的动作 pageMethod
		// 当前页 currentPage
		PageBean pageBean = PageHelper.getPager(currentPage, pageMethod, totalRows);
		List<ProductModel> list = p.getBrand(Category_id, pageBean.getStartRow(), pageBean.getPageSize());
		return list;
	}


	public List<ProductModel> getFashionList() {
		ProductDao p = new ProductDaoImpl();
		List<ProductModel> products = p.getFashionList();
		return products;
	}


	public List<ProductModel> getNewproductList() {
		ProductDao p = new ProductDaoImpl();
		List<ProductModel> products = p.getNewproductList();
		return products;
	}


	public ProductModel getProduct(String product_id) {
		ProductDao p = new ProductDaoImpl();
		ProductModel productModel = new ProductModel();
		productModel = p.getProductText(product_id);
		return productModel;
	}


	public int getProductQuantity(String product_id) {
		ProductDao p = new ProductDaoImpl();
		int quantity = p.getQuantity(product_id);
		return quantity;
	}


	public String getProductName(String ategory_id) {
		ProductDao p = new ProductDaoImpl();
		String name = p.getName(ategory_id);
		return name;
	}


	public List<ProductModel> getBrowseList() {
		ProductDao p = new ProductDaoImpl();
		List<ProductModel> products = p.getBrowseList();
		products = p.getBrowseList();
		return products;
	}



	public String getQu(String id) {
		ProductDao p = new ProductDaoImpl();
		String st = p.getStock(id);
		return st;
	}



	public List<BrandModel> getBrandTextList() {
		ProductDao p = new ProductDaoImpl();
		List<BrandModel> products = p.getBrandModel();
		return products;

	}


}
